﻿#region DaRefereeCouncil
/*----------------------------------------------------------------
// 文件名：DaRefereeCouncil.cs
// 功能描述：类RefereeCouncil的数据访问层
//
// 
// 创建时间：2009-07-10 DtataAccess template . Ver 5.0.20090413
//
// 修改标识： 
// 修改描述： 
//----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Globalization;
using System.Text;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

using Las.Model; 


namespace Las.Dal
{
	///可以继承更上层接口,IRefereeCouncil ,数据访问层基类DaBase，
	///DaBase中包含了DbException异常处理静态方法
    public	partial  class DaRefereeCouncil:DaBase<MoRefereeCouncil>
    {	 
		#region SQL const
		internal const string TABLE_COLUMNS=" CHARGE_BRANCH_ID ,CHARGE_TYPE ,PERSON_ID ,MILITARY_ID ,FULLNAME ,SEX ,FROM_BRANCH_ID ,POSITION ,CLASS1 ,CLASS2   ";
		
		internal const string SQL_INSERT="INSERT INTO LA_REFEREE_COUNCIL ("+TABLE_COLUMNS+") VALUES (@ChargeBranchId,@ChargeType,@PersonId,@MilitaryId,@Fullname,@Sex,@FromBranchId,@Position,@Class1,@Class2)";
		internal const string SQL_SELECT="SELECT "+TABLE_COLUMNS+" FROM LA_REFEREE_COUNCIL  ";
		internal const string SQL_SELECT_ONE=SQL_SELECT+" WHERE CHARGE_BRANCH_ID=@ChargeBranchId AND  CHARGE_TYPE=@ChargeType AND  PERSON_ID=@PersonId";                              
		
		internal const string SQL_EXIST="SELECT COUNT(*) FROM LA_REFEREE_COUNCIL  WHERE PERSON_ID=@PersonId and  CHARGE_BRANCH_ID=@ChargeBranchId and  CHARGE_TYPE=@ChargeType  ";
		internal const string SQL_UPDATE="UPDATE LA_REFEREE_COUNCIL SET MILITARY_ID=@MilitaryId, FULLNAME=@Fullname, SEX=@Sex, FROM_BRANCH_ID=@FromBranchId, POSITION=@Position, CLASS1=@Class1, CLASS2=@Class2 WHERE CHARGE_BRANCH_ID=@ChargeBranchId AND  CHARGE_TYPE=@ChargeType AND  PERSON_ID=@PersonId";
		
		internal const string SQL_DELETE_DEFAULT = "DELETE FROM LA_REFEREE_COUNCIL ";
		internal const string SQL_DELETE="DELETE FROM LA_REFEREE_COUNCIL WHERE CHARGE_BRANCH_ID=@ChargeBranchId AND  CHARGE_TYPE=@ChargeType AND  PERSON_ID=@PersonId";
		
		internal const string SQL_COUNT="SELECT COUNT(*) FROM LA_Referee_Council";
		#endregion
		       
		#region Constructor
	 
		/// <summary>
        /// 默认构造函数，使用配置文件中默认的数据库配置。
        /// </summary>
		public DaRefereeCouncil ()
		{ 
			this.db=DatabaseFactory.CreateDatabase();
		}
		/// <summary>
        /// 使用指定的databaseName数据库配置。
        /// </summary>
        /// <param name="databaseName">配置文件中数据库的配置名称。</param>
		public DaRefereeCouncil (string databaseName)
		{ 
            this.db = DatabaseFactory.CreateDatabase(databaseName);
		} 
		
		public DaRefereeCouncil (Database db)
		{ 
            this.db = db;
		} 
		
		#endregion
		
		//以下是CRUD方法，每个方法都有引入外部事务的版本

		#region 构造器模块
		
        protected override MoRefereeCouncil ConstructT()
        {
            return new MoRefereeCouncil();
        }
        #endregion
		
	    
		
		#region Add模块 Helper
		
		///build the command object.It never throw exception.
		protected override  DbCommand ConstructAddCommand(Database db)
		{
			DbCommand dbCommand = db.GetSqlStringCommand(SQL_INSERT);
			 
			db.AddInParameter(dbCommand,"ChargeBranchId",DbType.AnsiString);
			db.AddInParameter(dbCommand,"ChargeType",DbType.Int32);
			db.AddInParameter(dbCommand,"PersonId",DbType.AnsiString);
			db.AddInParameter(dbCommand,"MilitaryId",DbType.AnsiString);
			db.AddInParameter(dbCommand,"Fullname",DbType.String);
			db.AddInParameter(dbCommand,"Sex",DbType.Int32);
			db.AddInParameter(dbCommand,"FromBranchId",DbType.AnsiString);
			db.AddInParameter(dbCommand,"Position",DbType.String);
			db.AddInParameter(dbCommand,"Class1",DbType.AnsiString);
			db.AddInParameter(dbCommand,"Class2",DbType.AnsiString);
			return dbCommand;
		}		
		protected override DbCommand ConstructAddCommand(Database db,MoRefereeCouncil entity)
		{
			DbCommand dbCommand=db.GetSqlStringCommand(SQL_INSERT);			 
			db.AddInParameter(dbCommand,"ChargeBranchId",DbType.AnsiString,entity.ChargeBranchId);
			db.AddInParameter(dbCommand,"ChargeType",DbType.Int32,entity.ChargeType);
			db.AddInParameter(dbCommand,"PersonId",DbType.AnsiString,entity.PersonId);
			db.AddInParameter(dbCommand,"MilitaryId",DbType.AnsiString,entity.MilitaryId);
			db.AddInParameter(dbCommand,"Fullname",DbType.String,entity.Fullname);
			db.AddInParameter(dbCommand,"Sex",DbType.Int32,entity.Sex);
			db.AddInParameter(dbCommand,"FromBranchId",DbType.AnsiString,entity.FromBranchId);
			db.AddInParameter(dbCommand,"Position",DbType.String,entity.Position);
			db.AddInParameter(dbCommand,"Class1",DbType.AnsiString,entity.Class1);
			db.AddInParameter(dbCommand,"Class2",DbType.AnsiString,entity.Class2);
			return dbCommand;
		}
		
		protected override DbCommand PopulateAddCommandParameters(Database db,DbCommand addCmd,MoRefereeCouncil entity)
		{
			db.SetParameterValue(addCmd,"ChargeBranchId",entity.ChargeBranchId);
			db.SetParameterValue(addCmd,"ChargeType",entity.ChargeType);
			db.SetParameterValue(addCmd,"PersonId",entity.PersonId);
			db.SetParameterValue(addCmd,"MilitaryId",entity.MilitaryId);
			db.SetParameterValue(addCmd,"Fullname",entity.Fullname);
			db.SetParameterValue(addCmd,"Sex",entity.Sex);
			db.SetParameterValue(addCmd,"FromBranchId",entity.FromBranchId);
			db.SetParameterValue(addCmd,"Position",entity.Position);
			db.SetParameterValue(addCmd,"Class1",entity.Class1);
			db.SetParameterValue(addCmd,"Class2",entity.Class2);
			return addCmd;
		}
		
		protected override DbCommand PopulateAddCommandParameters(Database db,DbCommand addCmd,DataRow row)
		{
			db.SetParameterValue(addCmd,"ChargeBranchId",row["ChargeBranchId"]);
			db.SetParameterValue(addCmd,"ChargeType",row["ChargeType"]);
			db.SetParameterValue(addCmd,"PersonId",row["PersonId"]);
			db.SetParameterValue(addCmd,"MilitaryId",row["MilitaryId"]);
			db.SetParameterValue(addCmd,"Fullname",row["Fullname"]);
			db.SetParameterValue(addCmd,"Sex",row["Sex"]);
			db.SetParameterValue(addCmd,"FromBranchId",row["FromBranchId"]);
			db.SetParameterValue(addCmd,"Position",row["Position"]);
			db.SetParameterValue(addCmd,"Class1",row["Class1"]);
			db.SetParameterValue(addCmd,"Class2",row["Class2"]);
			return addCmd;
		}
		
		#endregion
		    
		#region Update模块 Helper
		
		protected override DbCommand ConstructUpdateCommand(Database db)
		{
			DbCommand dbCommand=db.GetSqlStringCommand(SQL_UPDATE);		    
			db.AddInParameter(dbCommand,"MilitaryId",DbType.AnsiString);
			db.AddInParameter(dbCommand,"Fullname",DbType.String);
			db.AddInParameter(dbCommand,"Sex",DbType.Int32);
			db.AddInParameter(dbCommand,"FromBranchId",DbType.AnsiString);
			db.AddInParameter(dbCommand,"Position",DbType.String);
			db.AddInParameter(dbCommand,"Class1",DbType.AnsiString);
			db.AddInParameter(dbCommand,"Class2",DbType.AnsiString);
			db.AddInParameter(dbCommand,"PersonId",DbType.AnsiString);
			db.AddInParameter(dbCommand,"ChargeBranchId",DbType.AnsiString);
			db.AddInParameter(dbCommand,"ChargeType",DbType.Int32);
			 
			return dbCommand;
		}
		protected override DbCommand ConstructUpdateCommand(Database db,MoRefereeCouncil entity)
		{
			DbCommand dbCommand=db.GetSqlStringCommand(SQL_UPDATE);		    
			db.AddInParameter(dbCommand,"MilitaryId",DbType.AnsiString,entity.MilitaryId);
			db.AddInParameter(dbCommand,"Fullname",DbType.String,entity.Fullname);
			db.AddInParameter(dbCommand,"Sex",DbType.Int32,entity.Sex);
			db.AddInParameter(dbCommand,"FromBranchId",DbType.AnsiString,entity.FromBranchId);
			db.AddInParameter(dbCommand,"Position",DbType.String,entity.Position);
			db.AddInParameter(dbCommand,"Class1",DbType.AnsiString,entity.Class1);
			db.AddInParameter(dbCommand,"Class2",DbType.AnsiString,entity.Class2);
			db.AddInParameter(dbCommand,"PersonId",DbType.AnsiString,entity.PersonId);
			db.AddInParameter(dbCommand,"ChargeBranchId",DbType.AnsiString,entity.ChargeBranchId);
			db.AddInParameter(dbCommand,"ChargeType",DbType.Int32,entity.ChargeType);
			 
			return dbCommand;
		}
		
		protected override DbCommand PopulateUpdateCommandParameters(Database db,DbCommand updateCmd,MoRefereeCouncil entity)
		{
			db.SetParameterValue(updateCmd,"MilitaryId",entity.MilitaryId);
			db.SetParameterValue(updateCmd,"Fullname",entity.Fullname);
			db.SetParameterValue(updateCmd,"Sex",entity.Sex);
			db.SetParameterValue(updateCmd,"FromBranchId",entity.FromBranchId);
			db.SetParameterValue(updateCmd,"Position",entity.Position);
			db.SetParameterValue(updateCmd,"Class1",entity.Class1);
			db.SetParameterValue(updateCmd,"Class2",entity.Class2);
			db.SetParameterValue(updateCmd,"PersonId",entity.PersonId);
			db.SetParameterValue(updateCmd,"ChargeBranchId",entity.ChargeBranchId);
			db.SetParameterValue(updateCmd,"ChargeType",entity.ChargeType);
			return updateCmd;
		}
		protected override DbCommand PopulateUpdateCommandParameters(Database db,DbCommand updateCmd,DataRow row)
		{
			db.SetParameterValue(updateCmd,"MilitaryId",row["MilitaryId"]);
			db.SetParameterValue(updateCmd,"Fullname",row["Fullname"]);
			db.SetParameterValue(updateCmd,"Sex",row["Sex"]);
			db.SetParameterValue(updateCmd,"FromBranchId",row["FromBranchId"]);
			db.SetParameterValue(updateCmd,"Position",row["Position"]);
			db.SetParameterValue(updateCmd,"Class1",row["Class1"]);
			db.SetParameterValue(updateCmd,"Class2",row["Class2"]);
			db.SetParameterValue(updateCmd,"PersonId",row["PersonId"]);
			db.SetParameterValue(updateCmd,"ChargeBranchId",row["ChargeBranchId"]);
			db.SetParameterValue(updateCmd,"ChargeType",row["ChargeType"]);
			return updateCmd;
		}
		
		protected override void PrepareDataAdapterCommand(Database db,out DbCommand dbInsertCommand,
		                     out DbCommand dbUpdateCommand,out DbCommand dbDeleteCommand)
		{
			dbInsertCommand = db.GetSqlStringCommand(SQL_INSERT);
			#region set insert cmd parameters
			db.AddInParameter(dbInsertCommand, "ChargeBranchId",DbType.AnsiString, "CHARGE_BRANCH_ID", DataRowVersion.Current);
			db.AddInParameter(dbInsertCommand, "ChargeType",DbType.Int32, "CHARGE_TYPE", DataRowVersion.Current);
			db.AddInParameter(dbInsertCommand, "PersonId",DbType.AnsiString, "PERSON_ID", DataRowVersion.Current);
			db.AddInParameter(dbInsertCommand, "MilitaryId",DbType.AnsiString, "MILITARY_ID", DataRowVersion.Current);
			db.AddInParameter(dbInsertCommand, "Fullname",DbType.String, "FULLNAME", DataRowVersion.Current);
			db.AddInParameter(dbInsertCommand, "Sex",DbType.Int32, "SEX", DataRowVersion.Current);
			db.AddInParameter(dbInsertCommand, "FromBranchId",DbType.AnsiString, "FROM_BRANCH_ID", DataRowVersion.Current);
			db.AddInParameter(dbInsertCommand, "Position",DbType.String, "POSITION", DataRowVersion.Current);
			db.AddInParameter(dbInsertCommand, "Class1",DbType.AnsiString, "CLASS1", DataRowVersion.Current);
			db.AddInParameter(dbInsertCommand, "Class2",DbType.AnsiString, "CLASS2", DataRowVersion.Current);
			#endregion
			dbUpdateCommand = db.GetSqlStringCommand(SQL_UPDATE);
			#region Set update cmd value parameters
			db.AddInParameter(dbUpdateCommand, "MilitaryId", DbType.AnsiString, "MILITARY_ID", DataRowVersion.Current);
			db.AddInParameter(dbUpdateCommand, "Fullname", DbType.String, "FULLNAME", DataRowVersion.Current);
			db.AddInParameter(dbUpdateCommand, "Sex", DbType.Int32, "SEX", DataRowVersion.Current);
			db.AddInParameter(dbUpdateCommand, "FromBranchId", DbType.AnsiString, "FROM_BRANCH_ID", DataRowVersion.Current);
			db.AddInParameter(dbUpdateCommand, "Position", DbType.String, "POSITION", DataRowVersion.Current);
			db.AddInParameter(dbUpdateCommand, "Class1", DbType.AnsiString, "CLASS1", DataRowVersion.Current);
			db.AddInParameter(dbUpdateCommand, "Class2", DbType.AnsiString, "CLASS2", DataRowVersion.Current);
			#endregion
			#region set update cmd pk where parameters
			db.AddInParameter(dbUpdateCommand, "PersonId", DbType.AnsiString, "PERSON_ID", DataRowVersion.Current);
			db.AddInParameter(dbUpdateCommand, "ChargeBranchId", DbType.AnsiString, "CHARGE_BRANCH_ID", DataRowVersion.Current);
			db.AddInParameter(dbUpdateCommand, "ChargeType", DbType.Int32, "CHARGE_TYPE", DataRowVersion.Current);
			#endregion			
			dbDeleteCommand = db.GetSqlStringCommand(SQL_DELETE);
			#region set delete cmd pk where parameters
			db.AddInParameter(dbDeleteCommand, "PersonId", DbType.AnsiString, "PERSON_ID", DataRowVersion.Current);
			db.AddInParameter(dbDeleteCommand, "ChargeBranchId", DbType.AnsiString, "CHARGE_BRANCH_ID", DataRowVersion.Current);
			db.AddInParameter(dbDeleteCommand, "ChargeType", DbType.Int32, "CHARGE_TYPE", DataRowVersion.Current);
			#endregion
		}
		#endregion	
		
		#region Delete模块 Helper		
		
		protected override DbCommand ConstructDeleteCommand(Database db)
		{
			DbCommand dbCommand=db.GetSqlStringCommand(SQL_DELETE);
			 
			db.AddInParameter(dbCommand,"PersonId",DbType.AnsiString);
			db.AddInParameter(dbCommand,"ChargeBranchId",DbType.AnsiString);
			db.AddInParameter(dbCommand,"ChargeType",DbType.Int32);
			 
			return dbCommand;
		}		
		protected override DbCommand ConstructDeleteCommand(Database db,MoRefereeCouncil entity)
		{
			DbCommand dbCommand=db.GetSqlStringCommand(SQL_DELETE);
			 
			db.AddInParameter(dbCommand,"PersonId",DbType.AnsiString,entity.PersonId);
			db.AddInParameter(dbCommand,"ChargeBranchId",DbType.AnsiString,entity.ChargeBranchId);
			db.AddInParameter(dbCommand,"ChargeType",DbType.Int32,entity.ChargeType);
		 
			return dbCommand;
		}
		
		protected override DbCommand ConstructDeleteCommandForWhere(Database db, string where)
        {
            return db.GetSqlStringCommand(SQL_DELETE_DEFAULT + where);            
        }

        protected override DbCommand PopulateDeleteCommandParameters(Database db, DbCommand deleteCmd, MoRefereeCouncil entity)
        {
			db.SetParameterValue(deleteCmd,"PersonId",entity.PersonId);
			db.SetParameterValue(deleteCmd,"ChargeBranchId",entity.ChargeBranchId);
			db.SetParameterValue(deleteCmd,"ChargeType",entity.ChargeType);
			return deleteCmd;
        }

        protected override DbCommand PopulateDeleteCommandParameters(Database db, DbCommand deleteCmd, DataRow row)
        {
			db.SetParameterValue(deleteCmd,"PersonId",row["PersonId"]);
			db.SetParameterValue(deleteCmd,"ChargeBranchId",row["ChargeBranchId"]);
			db.SetParameterValue(deleteCmd,"ChargeType",row["ChargeType"]);
			return deleteCmd;
        }      
		
		#endregion helper
		
		#region Query模块 Helper

        protected override DbCommand ConstructQueryCommand(string condition)
        {
            return db.GetSqlStringCommand(SQL_SELECT + condition);
        }

        #endregion
		
		#region GetEntity(s)模块 Helper
        
		protected override DbCommand ConstructSelectOneCommand(Database db,MoRefereeCouncil entity)
		{
			DbCommand dbCommand=db.GetSqlStringCommand(SQL_SELECT_ONE);
			 
			db.AddInParameter(dbCommand,"PersonId",DbType.AnsiString,entity.PersonId);
			db.AddInParameter(dbCommand,"ChargeBranchId",DbType.AnsiString,entity.ChargeBranchId);
			db.AddInParameter(dbCommand,"ChargeType",DbType.Int32,entity.ChargeType);
			return dbCommand;
		}
		
		protected override void PopulateEntityByDataReader(IDataReader reader,ref MoRefereeCouncil entity)
		{
			if (!reader.IsDBNull(0)) entity.ChargeBranchId = reader.GetString(0);
			if (!reader.IsDBNull(1)) entity.ChargeType = reader.GetInt32(1);
			if (!reader.IsDBNull(2)) entity.PersonId = reader.GetString(2);
			if (!reader.IsDBNull(3)) entity.MilitaryId = reader.GetString(3);
			if (!reader.IsDBNull(4)) entity.Fullname = reader.GetString(4);
			if (!reader.IsDBNull(5)) entity.Sex = reader.GetInt32(5);
			if (!reader.IsDBNull(6)) entity.FromBranchId = reader.GetString(6);
			if (!reader.IsDBNull(7)) entity.Position = reader.GetString(7);
			if (!reader.IsDBNull(8)) entity.Class1 = reader.GetString(8);
			if (!reader.IsDBNull(9)) entity.Class2 = reader.GetString(9);
		}
		
		

        protected override DbCommand ConstructSelectConditionCommand(Database db, string condition)
        {
            return db.GetSqlStringCommand(SQL_SELECT + condition);
        }
		
		#endregion	
		
		#region Count模块 Helper

        protected override DbCommand ConstructCountCommand(Database db)
        {
            return db.GetSqlStringCommand(SQL_COUNT);
        }

        protected override DbCommand ConstructCountConditionCommand(Database db, string condition)
        {
            return db.GetSqlStringCommand(SQL_COUNT + condition);
        }

		#endregion
		
		#region IsExist模块 Helper

        protected override DbCommand ConstructIsExistCommand(Database db, MoRefereeCouncil entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_EXIST);
			db.AddInParameter(dbCommand,"PersonId",DbType.AnsiString,entity.PersonId);
			db.AddInParameter(dbCommand,"ChargeBranchId",DbType.AnsiString,entity.ChargeBranchId);
			db.AddInParameter(dbCommand,"ChargeType",DbType.Int32,entity.ChargeType);
			return dbCommand;
        }

		#endregion

		
		
	}
}	
#endregion

